package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //查询所有攻略
        List<Strategy> strategies = strategyService.list();
        model.addAttribute("strategies", strategies);
        //查询所有主题
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);
        //普通属性的回显,和内容的回显
        if (id != null) {
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            StrategyContent content = strategyDetailService.getContent(id);
            strategyDetail.setStrategyContent(content);
            model.addAttribute("strategyDetail", strategyDetail);
            //标签的回显 从数据库查出来
            model.addAttribute("tags", strategyTagService.getTagString(strategyDetail.getId()));
        }
        return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId) {
        //根据攻略文章id查询分类
        List<StrategyCatalog> strategyCatalogs = strategyService.queryCatalogByStrategyId(strategyId);
        return strategyCatalogs;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags) {
        strategyDetailService.saveOrUpdate(strategyDetail,tags);
        return AjaxResult.success();
    }
}
